package org.baicaix.demo {
	import flash.sampler.NewObjectSample;
	import org.baicaix.game.worms.map.BattleMap;

	import org.baicaix.pool.ObjectPoolManager;

	import flash.display.BitmapData;
	import flash.display.BlendMode;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.ColorTransform;
	import flash.geom.Matrix;
	import flash.geom.Point;

	/**
	 * @author baicaix
	 */
	public class WildFireDemo extends Sprite {

		private var fires : Array;

		public function WildFireDemo() {
			
			fires = [];
			ObjectPoolManager.getInstance().init(stage);
			
			initStage();
			initEventListener();
			
			var map : BattleMap = this.addChild(BattleMap.getInstance()) as BattleMap;
			map.y = 100;
		}

		/**
		 * 方法初始化stage数据
		 */
		private function initStage() : void {
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
		}

		private function initEventListener() : void {
			stage.addEventListener(MouseEvent.CLICK, clickHandler);
			stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
		}

		private function clickHandler(event : MouseEvent) : void {
			for (var i : int = 0;i < 10;i++) {
				var fire : WildFire = ObjectPoolManager.getInstance().get(WildFire);
				fire.fire();
				fire.x = event.stageX;
				fire.y = event.stageY;
				fires.push(fire);
			}
		}

		private const zhongli : Number = 1;
		private var fall : Boolean = true;
		private function enterFrameHandler(event : Event) : void {
			for each (var fire : WildFire in fires) {
				var mapBmData : BitmapData = BattleMap.getCurrentBitmapData();
				var p : Point = new Point(0, 0);
				p = fire.localToGlobal(p);
	            
				if(fall && !mapBmData.hitTest(BattleMap.getHitFirstPoint(), 0xFF, p)) {
					fire.vy += zhongli;
				} else {
					fall = false;
					fire.vx = Math.random() * (Math.random() > .5 ? 1 : -1) * 3;
					fire.vy = Math.random() * (Math.random() > .5 ? 1 : -1) * 3;
					
					//绘制一个坐标在当前为止的虚拟火焰，用于擦除
					var localPoint:Point = BattleMap.getInstance().globalToLocal(new Point(fire.x, fire.y));
            
		            var earseShape:Shape = new Shape();    
		            earseShape.graphics.beginFill(0xFFFFFF);
		            earseShape.graphics.drawCircle(localPoint.x, localPoint.y, 2);
		            earseShape.graphics.endFill();
					
					mapBmData.draw(earseShape, null, null, BlendMode.ERASE);
//					mapBmData.merge(sourceBitmapData, sourceRect, destPoint, redMultiplier, greenMultiplier, blueMultiplier, alphaMultiplier)

					earseShape.graphics.clear();
				}
				//碰到，此时应该烧灼，并且随机运动
				fire.x += fire.vx;
				fire.y += fire.vy;
			}
		}
	}
}

import flash.display.Sprite;

class GameInfo extends Sprite {
	
//	private var fire : TextField;
//
//	public function GameInfo() {
//		fire = this.addChild(new TextField()) as TextField;
//	}
//
//	private function fire(num : int) : void {
//	}
}
